<!doctype html>

<html lang="<%=page.language%>">

<% include header.ejs %>

<body id="users">

	<script src="<%=cstatic%>/js/lib/jquery.validate<%=min%>.js"></script>
	<script src="<%=cstatic%>/js/lib/localization/messages_<%=page.language%>.js"></script>
	
	<script>
        $(document).ready( function() {
            $("#records td.edit a").prepend("<span class='icon-pencil'></span>");

            $("#doNew").button({ icons: { primary: "ui-icon-plus"}, text: true}).click( function() {
                $("#request").val("new");
                $("#search").submit();
            });
            $("#doSave").button({ icons: { primary: "ui-icon-check"}, text: true}).click( function() {
                $("#request").val("save");
                $("#record").submit();
            });
            $("#doDelete").button({ icons: { primary: "ui-icon-trash"}, text: true}).click( function() {
                $("#request").val("delete");
                $("#record").submit();
            });
            $("#doCancel").button({ icons: { primary: "ui-icon-close"}, text: true}).click( function() {
                $("#request").val("list");
                $("#record").submit();
            });
            $("#domains").change(function() {
                $('#domain').val(this.options[this.selectedIndex].value);
            });

            $("#record").validate();
        });
	</script>

    <section>
        <header>
            <% include top.ejs %>
            <% include navigation.ejs %>
        </header>

    <div>

        <% if (typeof user != "undefined") { %>
            <form action="./users" method="post" id="record">
            <input type="hidden" name="request" id="request" value="save" />
            <input type="hidden" name="id" value="<%- user.id %>" />

            <fieldset class="horizontal">
        <div>
            <label for="name"><%= __("Name")%></label>
            <input name="name" id="name" type="text" value="<%- user.name %>" class="required"/>
        </div>
        <div>
            <label for="username"><%= __("Login")%></label>
            <input name="username" id="username" type="text" value="<%- user.username %>" class="required" />
        </div>
        <div>
            <label for="password"><%= __("Password")%></label>
            <input name="password" id="password" type="text" value="" <% if (typeof user.username === "undefined") { %> class="required" <% } %> />
            <% if (user.id > 0) { %>
                <span><%= __("Leave empty if you don't want to change your password.")%></span>
            <% } %>
        </div>
    <div>
        <label for="domain"><%= __("Group")%></label>
        <input name="domain" id="domain" type="text" value="<%- user.domain %>" />

        <select name="domains" id="domains">
            <option value=""><%= __("Choose an existing group or type a new one")%></option>
            <%- optionList(domains, -1) %>
        </select>
    </div>
    <div>
        <label for="level"><%= __("Level")%></label>
        <select name="level" id ="level">
          <%- optionList(levels, user.level) %>
        </select>
    </div>
    <div>
        <label for="active"><%= __("Active")%></label>
        <input name="active" id="activeY" type="radio" value="Y" <%= checked(user.active == 'Y') %> /><label class="choice" for="activeY">Yes</label>
        <input name="active" id="activeN" type="radio" value="N" <%= checked(user.active != 'Y') %> /><label class="choice" for="activeN">No</label>
    </div>
    <div>
        <label for="badlogins"><%= __("Bad logins")%> </label>
        <input name="badlogins" id="badlogins" type="text" class="number" value="<%- user.badlogins %>"  class="required number"/>

        <label for="maxbadlogins"><%= __("Max allowed")%> </label>
        <input name="maxbadlogins" id="maxbadlogins" type="text" class="number" value="<%- user.maxbadlogins %>"  class="required number"/>
    </div>
    <div>
        <label for="nomail"><%= __("Allows Mail")%> </label>
        <input name="nomail" id="nomailY" type="radio" value="Y" <%= checked(user.nomail == 'Y') %> /><label class="choice" for="nomailY">No</label>
        <input name="nomail" id="nomailN" type="radio" value="N" <%= checked(user.nomail != 'Y') %> /><label class="choice" for="nomailN">Yes</label>
    </div>
    <div>
        <label for="email"><%= __("Email")%></label>
        <input name="email" id="email" type="text" value="<%- user.email %>"  class="required email"/>
    </div>
    </fieldset>
    <div id="action_buttons">
        <button id="doSave"><%= __("Save")%></button>
        <% if (user.id > 0) { %>
            <button id="doDelete"><%= __("Delete")%></button>
        <% } %>
        <button id="doCancel"><%= __("Cancel")%></button>
    </div>
    </form>
    <% } %>


    <% if (typeof users != "undefined") { %>
        <form action="./users" method="post" id="search">
        <input type="hidden" name="request" id="request" value="list" />
        <!--TODO: add  search button + input field -->
        <button type="button"  class="btn btn-default" id="doNew"><%= __("New")%></button>

        <table id="records">
        <tr>
         <th><%= __("Naam")%></th><th><%= __("Login")%></th><th><%= __("Domain")%></th><th><%= __("Level")%></th><th><%= __("Login")%></th><th><%= __("Email")%></th>
        </tr>

        <% for (var i=0; i < users.length; i++) { var u = users[i]; %>
            <tr>
                <td class="edit"><a href="/<%=page.url%>/edit/<%= u.id %>"><%= (u.name==='') ? '--' : u.name %></a></td>
                <td><%= u.username %></td>
                <td><%= u.domain %></td>
                <td>
                  <%= find(levels, u.level).name %>
                </td>
                <td><%= (u.active == 'Y') ? 'active' : '-' %></td>
                <td><%= u.email %></td>
            </tr>
        <% } %>
    </table>
    </form>
    <% } %>

    </div>

    <% include footer.ejs %>
    </section>
</body>
</html>
